package com.sx.micowell.module.train.run.time

import android.util.Log
import android.view.Gravity
import android.widget.LinearLayout
import com.alibaba.android.arouter.facade.annotation.Route
import com.bigkoo.pickerview.configure.PickerOptions
import com.bigkoo.pickerview.listener.ISelectTimeCallback
import com.bigkoo.pickerview.view.WheelTime
import com.contrarywind.view.WheelView
import com.example.mvvm.base.data.DataBindingConfig
import com.sx.micowell.R
import com.sx.micowell.databinding.FragTrainRunTimeBinding
import com.sx.micowell.framework.app.MyFragment
import com.sx.micowell.framework.arouth.RoutePath.Companion.SPORT_TIME
import com.sx.micowell.module.MainActivity
import java.text.ParseException
import java.util.*


/**
 * @Description
 * @Author 梁浩文
 * @Date 2021/9/6 14:06
 */
@Route(path = SPORT_TIME)
class TrainRunTimeFragment : MyFragment<FragTrainRunTimeBinding, MainActivity>() {
    override fun getDataBindingConfig(): DataBindingConfig =
        DataBindingConfig(R.layout.frag_train_run_time)

    override fun initView() {
        mPickerOptions = PickerOptions(PickerOptions.TYPE_PICKER_TIME)
        mPickerOptions.lineSpacingMultiplier=5f
        mPickerOptions.itemsVisibleCount=3
        mPickerOptions.isAlphaGradient=true
        mPickerOptions.textGravity= Gravity.CENTER
        mPickerOptions.dividerType= WheelView.DividerType.WRAP //分隔线类型
        mPickerOptions.label_hours=""
        mPickerOptions.label_minutes=""
        mPickerOptions.type=booleanArrayOf(false, false, false, true, true, false)
        initWheelTime(binding.timepicker)
    }

    override fun initData() {

    }

    lateinit var wheelTime: WheelTime
    lateinit var mPickerOptions: PickerOptions
    private fun initWheelTime(timePickerView: LinearLayout) {
        wheelTime = WheelTime(
            timePickerView,
            mPickerOptions.type,
            mPickerOptions.textGravity,
            mPickerOptions.textSizeContent
        )
        if (mPickerOptions.timeSelectChangeListener != null) {
            wheelTime.setSelectChangeCallback(ISelectTimeCallback {
                try {
                    val date = WheelTime.dateFormat.parse(wheelTime.getTime())
                    mPickerOptions.timeSelectChangeListener.onTimeSelectChanged(date)
                } catch (e: ParseException) {
                    e.printStackTrace()
                }
            })
        }
        wheelTime.setLunarMode(mPickerOptions.isLunarCalendar)
        if (mPickerOptions.startYear != 0 && mPickerOptions.endYear != 0 && mPickerOptions.startYear <= mPickerOptions.endYear) {
            setRange()
        }

        //若手动设置了时间范围限制
        if (mPickerOptions.startDate != null && mPickerOptions.endDate != null) {
            require(mPickerOptions.startDate.getTimeInMillis() <= mPickerOptions.endDate.getTimeInMillis()) { "startDate can't be later than endDate" }
            setRangDate()
        } else if (mPickerOptions.startDate != null) {
            require(mPickerOptions.startDate.get(Calendar.YEAR) >= 1900) { "The startDate can not as early as 1900" }
            setRangDate()
        } else if (mPickerOptions.endDate != null) {
            require(mPickerOptions.endDate.get(Calendar.YEAR) <= 2100) { "The endDate should not be later than 2100" }
            setRangDate()
        } else { //没有设置时间范围限制，则会使用默认范围。
            setRangDate()
        }
        setTime()
        wheelTime.setLabels(
            mPickerOptions.label_year,
            mPickerOptions.label_month,
            mPickerOptions.label_day,
            mPickerOptions.label_hours,
            mPickerOptions.label_minutes,
            mPickerOptions.label_seconds
        )
        wheelTime.setTextXOffset(
            mPickerOptions.x_offset_year,
            mPickerOptions.x_offset_month,
            mPickerOptions.x_offset_day,
            mPickerOptions.x_offset_hours,
            mPickerOptions.x_offset_minutes,
            mPickerOptions.x_offset_seconds
        )
        wheelTime.setItemsVisible(mPickerOptions.itemsVisibleCount)
        wheelTime.setAlphaGradient(mPickerOptions.isAlphaGradient)
        wheelTime.setCyclic(mPickerOptions.cyclic)
        wheelTime.setDividerColor(mPickerOptions.dividerColor)
        wheelTime.setDividerType(mPickerOptions.dividerType)
        wheelTime.setLineSpacingMultiplier(mPickerOptions.lineSpacingMultiplier)
        wheelTime.setTextColorOut(mPickerOptions.textColorOut)
        wheelTime.setTextColorCenter(mPickerOptions.textColorCenter)
        wheelTime.isCenterLabel(mPickerOptions.isCenterLabel)
    }


    /**
     * 设置默认时间
     */
    fun setDate(date: Calendar?) {
        mPickerOptions.date = date
        setTime()
    }

    /**
     * 设置可以选择的时间范围, 要在setTime之前调用才有效果
     */
    private fun setRange() {
        wheelTime.startYear = mPickerOptions.startYear
        wheelTime.endYear = mPickerOptions.endYear
    }

    /**
     * 设置可以选择的时间范围, 要在setTime之前调用才有效果
     */
    private fun setRangDate() {
        wheelTime.setRangDate(mPickerOptions.startDate, mPickerOptions.endDate)
        initDefaultSelectedDate()
    }


    private fun initDefaultSelectedDate() {
        //如果手动设置了时间范围
        if (mPickerOptions.startDate != null && mPickerOptions.endDate != null) {
            //若默认时间未设置，或者设置的默认时间越界了，则设置默认选中时间为开始时间。
            if (mPickerOptions.date == null || mPickerOptions.date.timeInMillis < mPickerOptions.startDate.timeInMillis || mPickerOptions.date.timeInMillis > mPickerOptions.endDate.timeInMillis) {
                mPickerOptions.date = mPickerOptions.startDate
            }
        } else if (mPickerOptions.startDate != null) {
            //没有设置默认选中时间,那就拿开始时间当默认时间
            mPickerOptions.date = mPickerOptions.startDate
        } else if (mPickerOptions.endDate != null) {
            mPickerOptions.date = mPickerOptions.endDate
        }
    }


    /**
     * 设置选中时间,默认选中当前时间
     */
    private fun setTime() {
        val year: Int
        val month: Int
        val day: Int
        val hours: Int
        val minute: Int
        val seconds: Int
        val calendar = Calendar.getInstance()
        if (mPickerOptions.date == null) {
            calendar.timeInMillis = System.currentTimeMillis()
            year = calendar[Calendar.YEAR]
            month = calendar[Calendar.MONTH]
            day = calendar[Calendar.DAY_OF_MONTH]
            hours = calendar[Calendar.HOUR_OF_DAY]
            minute = calendar[Calendar.MINUTE]
            seconds = calendar[Calendar.SECOND]
        } else {
            year = mPickerOptions.date[Calendar.YEAR]
            month = mPickerOptions.date[Calendar.MONTH]
            day = mPickerOptions.date[Calendar.DAY_OF_MONTH]
            hours = mPickerOptions.date[Calendar.HOUR_OF_DAY]
            minute = mPickerOptions.date[Calendar.MINUTE]
            seconds = mPickerOptions.date[Calendar.SECOND]
        }
        wheelTime.setPicker(year, month, day, hours, minute, seconds)
    }

    fun returnData():Date {
        val date = WheelTime.dateFormat.parse(wheelTime.time)
//        var instance = Calendar.getInstance()
//        instance.time=date
//        var time=instance.get(Calendar.HOUR_OF_DAY)*60*1000L+instance.get(Calendar.MINUTE)*1000L
//        Log.i(TAG, "returnData: ${time}")
        return date
    }
}